package com.ody.util.code.model;

import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

public class POBean {
	private String name;
	private String title;
	private String table;
	private List<FieldBean> fields;
    private List<FieldBean> searchFields;
    private List<FieldBean> tableFields;
    private List<FieldBean> editFields;
    private List<FieldBean> importFields;
    private List<FieldBean> exportFields;
    
	private List<RelPOBean> relOneList = Lists.newArrayList();
    private List<RelPOBean> relManyList = Lists.newArrayList();
	
	public POBean() {}
	
	public POBean(String name, String table, String title) {
		this.name = name;
		this.table = table;
		this.title = title;
	}
	
	public List<String> getImportList(List<FieldBean> fields) {
		if (fields != null) {
			List<String> list = Lists.newArrayListWithExpectedSize(fields.size());
			for (FieldBean field : fields) {
				if (field.isRequireTypeImport() && !list.contains(field.getTypeFullName())) {
					list.add(field.getTypeFullName());
				}
			}
			return list;
		}
		return null;
	}
	
	public String getName() {
		return name;
	}
	public String getUncapitalizedName() {
		return StringUtils.uncapitalize(name);
	}
    public String getUncapName() {
        return StringUtils.uncapitalize(name);
    }
	public void setName(String name) {
		this.name = name;
	}
	public String getTable() {
		return table;
	}
	public void setTable(String table) {
		this.table = table;
	}
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
	public List<FieldBean> getFields() {
		return fields;
	}
	public void setFields(List<FieldBean> fields) {
		this.fields = fields;
	}
    public List<FieldBean> getSearchFields() {
        return searchFields;
    }
    public void setSearchFields(List<FieldBean> searchFields) {
        this.searchFields = searchFields;
    }
    public List<FieldBean> getTableFields() {
        return tableFields;
    }
    public void setTableFields(List<FieldBean> tableFields) {
        this.tableFields = tableFields;
    }
    public List<FieldBean> getEditFields() {
        return editFields;
    }
    public void setEditFields(List<FieldBean> editFields) {
        this.editFields = editFields;
    }
    public List<FieldBean> getImportFields() {
        return importFields;
    }
    public void setImportFields(List<FieldBean> importFields) {
        this.importFields = importFields;
    }
    public List<FieldBean> getExportFields() {
        return exportFields;
    }
    public void setExportFields(List<FieldBean> exportFields) {
        this.exportFields = exportFields;
    }
    
    public List<String> getFieldNames() {
        if (fields != null) {
            List<String> list = new ArrayList<>(fields.size());
            for (FieldBean field : fields) {
                list.add(field.getName());
            }
            return list;
        } else {
            return new ArrayList<>();
        }
    }
    
    public List<RelPOBean> getRelOneList() {
        return relOneList;
    }
    
    public List<RelPOBean> getRelManyList() {
        return relManyList;
    }
    
    public List<FieldBean> getCommonFields() {
        if (fields != null) {
            return fields.stream().filter(FieldBean::isCommon).map(FieldBean::clone).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
    
    public List<FieldBean> getNonCommonFields() {
        if (fields != null) {
            return fields.stream().filter(f -> !f.isCommon()).map(FieldBean::clone).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
    
    @Override
	public String toString() {
		return "name:" + name + ",title:" + title;
	}
    
    public boolean isIgnore(String ignoreModel) {
        return getName().equals(ignoreModel) || getTable().equals(ignoreModel);
    }
    
    public boolean isSearchable() {
        return CollectionUtils.isNotEmpty(searchFields);
    }
    public boolean isEditable() {
        return CollectionUtils.isNotEmpty(editFields);
    }
    public boolean isExportable() {
        return CollectionUtils.isNotEmpty(exportFields);
    }
    public boolean isImportable() {
        return CollectionUtils.isNotEmpty(importFields);
    }
}
